<?php
/**
 * Created by PhpStorm.
 * UsersF: Mloong
 * Date: 2019/2/27
 * Time: 11:31
 */

namespace app\login\model;


class Registered extends BaseModel
{
    protected $hidden = ['user_loan_money', 'user_loan_day', 'user_loan_get_money', 'user_periods'];

    # 把根据手机号码查询出来的数据写入到对应手机号码的记录里（单期）
    public function update_user_loan_d($phone, $user_loan_d)
    {
        $user_loan_money_d = $user_loan_d['money'];
        $user_loan_day_d = $user_loan_d['loan_day'];
        $user_loan_get_money_d = $user_loan_d['get_money'];
        $user_loan_get_status_d = $user_loan_d['status'];
        self::where('phone', '=', $phone)
            ->data(['user_loan_money'=> $user_loan_money_d, 'user_loan_day'=>$user_loan_day_d,
                'user_loan_get_money'=>$user_loan_get_money_d, 'loan_status'=>$user_loan_get_status_d])
            ->update();
    }

    # 把根据手机号码查询出来的数据写入到对应手机号码的记录里（分期）
    public function update_user_loan_f($phone, $user_loan_f)
    {
        $user_loan_money_f = $user_loan_f['money'];
        $user_loan_periods_f = $user_loan_f['periods'];
        $user_loan_get_money_f = $user_loan_f['get_money'];
        $user_loan_get_status_f = $user_loan_f['status'];
        self::where('phone', '=', $phone)
            ->data(['user_loan_money'=> $user_loan_money_f, 'user_periods'=>$user_loan_periods_f,
                'user_loan_get_money'=>$user_loan_get_money_f,'loan_status'=>$user_loan_get_status_f])
            ->update();
    }

    public function getLoanStatusAttr($value, $data)
    {
        if($data['type'] == 'd'){
            $arr = config('status.d');
            return $arr[$value];
        }
        if($data['type'] == 'f'){
            $arr = config('status.f');
            return $arr[$value];
        }
    }

    # 根据name查找当天所有的注册记录
    public function getAll($from)
    {
        return self::whereTime('registered_time', 'today')
            ->where('from', '=', $from)
            ->select()
            ->toJson();
    }

    public function getPhoneAttr($value)
    {
        $phone = substr_replace($value, '****', 3,4);
        return $value = $phone;
    }
}